﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Diagnostics;
using System.IO;
using Debug = UnityEngine.Debug;
public class GetKeyFPrefabs : MonoBehaviour {
    public GameObject forward_meg;
    public GameObject backward_meg;
    public GameObject turnleft_meg;
    public GameObject turnright_meg;
    public GameObject carbreak_meg;
    public GameObject turbo_meg;

    public bool success = false;
    string forward, backward, turnleft, turnright, carbreak, turbo;
    List<string> meg_from_prefab = new List<string> { "","","","","","",""};
    List<string> name = new List<string>() { "W", "A", "S", "D", "Up Arrow", "Down Arrow", "Left Arrow", "Right Arrow", "Space", "J" };
    List<KeyCode> key_code = new List<KeyCode>() { KeyCode.None, KeyCode.None, KeyCode.None, KeyCode.None, KeyCode.None ,KeyCode.None};//, KeyCode.DownArrow,KeyCode.LeftArrow,KeyCode.RightArrow,KeyCode.Space,KeyCode.J};
    // Use this for initialization
    Process p;
    void Start()
    {

       
    }

    IEnumerator Runing()
    {

        //ProcessStartInfo proc = new ProcessStartInfo ("python", "/Users/haikunhuang/Dropbox/Python/test.py");
        //ProcessStartInfo proc = new ProcessStartInfo ("python", "/Users/haikunhuang/Dropbox/Python/Learning.py");
        ProcessStartInfo proc = new ProcessStartInfo("C:/Program Files (x86)/护眼宝/HuYanBao.exe", "1");

        proc.UseShellExecute = false;
        proc.CreateNoWindow = true;
        proc.WindowStyle = ProcessWindowStyle.Normal;
        proc.RedirectStandardOutput = true;

        Debug.Log("Start");

        p = Process.Start(proc);
        StreamReader sr = p.StandardOutput;

        while (!p.HasExited)
        {
            Debug.Log("running");
            yield return null;

        }

        Debug.Log("Done. Exiting code: " + p.ExitCode);

        while (!sr.EndOfStream)
        {
            Debug.Log("Output: " + sr.ReadLine());
        }

    }
 
	
	// Update is called once per frame
	void Update () {
        meg_from_prefab[0] = forward_meg.transform.GetChild(3).GetComponent<Text>().text;
        meg_from_prefab[1] = backward_meg.transform.GetChild(3).GetComponent<Text>().text;
        meg_from_prefab[2] = turnleft_meg.transform.GetChild(3).GetComponent<Text>().text;
        meg_from_prefab[3] = turnright_meg.transform.GetChild(3).GetComponent<Text>().text;
        meg_from_prefab[4] = carbreak_meg.transform.GetChild(3).GetComponent<Text>().text;
        meg_from_prefab[5] = turbo_meg.transform.GetChild(3).GetComponent<Text>().text;
        //for (int i = 0;i<=5;i++)
        //{
        //    Debug.Log(meg_from_prefab[i]);
        //}
    }
    public void apply_codes()
    {
        key_code[0] = forward_meg.transform.GetChild(5).GetComponent<DropDown>().current_code;
        key_code[1] = backward_meg.transform.GetChild(5).GetComponent<DropDown>().current_code;
        key_code[2] = turnleft_meg.transform.GetChild(5).GetComponent<DropDown>().current_code;
        key_code[3] = turnright_meg.transform.GetChild(5).GetComponent<DropDown>().current_code;
        key_code[4] = carbreak_meg.transform.GetChild(5).GetComponent<DropDown>().current_code;
        key_code[5] = turbo_meg.transform.GetChild(5).GetComponent<DropDown>().current_code;
        success = true;
        GameObject.Find("Text_Apply").GetComponent<Text>().text = "All Code has been Applied!";
        StartCoroutine(Runing());
    }
    
}
